/*
重庆时时彩 五星
*/
cc.Class({
    extends: cc.Component,

    properties: {
        rtDec:cc.RichText,//说明
        betContent:[cc.Node],//投注选择面板
        fbSelBall:cc.Prefab,//选择球
        ndMiss:[cc.Node],//遗漏显示
        sscPop:cc.Node,//根节点

        rules:[],//规则
        nums:[],//数值
        _curTopTitle:"五星直选",//当前玩法名
        _lotteryID:0,//彩种id
        _curSelects:[],//选择球数字列表
        _isMiss:false,//是否开启遗漏
        _curPage:0,
        _decContents:[],//说明列表
        _sscManage:null,
        //_missData:[],
    },

     //重置初始值
    initReset:function(){
    },

    //外部传参
    init:function(lotteryid){
        this._lotteryID = lotteryid;  
    },
    
    showMiss:function(data){
       // this._missData = data.slice();
        var dataTemp = data[0];
        for(var i = 0;i<this.ndMiss.length;i++)
        {
            var childs = this.ndMiss[i].children;
            for(var j=0;j<childs.length;j++)
            {
                childs[j].getComponent(cc.Label).string = dataTemp[i][j];
            }
            this.ndMiss[i].active = true;
        }
    },

    // showCurMiss:function(){
    //     var dataTemp = this._missData[parseInt(this._curPage)];
    //     for(var i = 0;i<this.ndMiss.length;i++)
    //     {
    //         var childs = this.ndMiss[i].children;
    //         for(var j=0;j<childs.length;j++)
    //         {
    //             childs[j].getComponent(cc.Label).string = dataTemp[i][j];
    //         }
    //         this.ndMiss[i].active = true;
    //     }
    // },

    hideMiss:function(){
        for(var i = 0;i<this.ndMiss.length;i++)
        {
            this.ndMiss[i].active = false;
        }
    },

     // use this for initialization
    start: function () {
        this._sscManage = this.sscPop.getComponent(this.sscPop.name);
        if(this._sscManage == null && this.betContent.length != 5 )
        {
            ComponentsUtils.showTips("投注面板加载失败");
            return;
        }
        this.rules = [DEFINE.LOTTERYRULESSC.ZHI_FIVE,DEFINE.LOTTERYRULESSC.TONG_FIVE];

        this.nums = ["0","1","2","3","4","5","6","7","8","9"]; 
         this._decContents = [
            "<color=#ffffff>每位至少选1个号码,按位猜对全部开奖号码即中</c><color=#fcd346>10万元宝</color>",
            "<color=#ffffff>每位至少选1个号码，按位猜对开奖号码可中</c><color=#fcd346>20~20440元宝</color>",
        ];

        this._lotteryID = DEFINE.LOTTERY_ID.SSC_CQ;
        for(var i=0;i<5;i++)
        {
            var arry = [];
            this._curSelects.push(arry);
        }
        for(var i=0;i<5;i++)
        {
            for(var j = 0;j<this.nums.length;j++)
            {      
                var ball = cc.instantiate(this.fbSelBall);
                ball.getComponent(ball.name).init(j);
                WidgetExtUtils.addCheckEvent(ball,this.node,this.node.name,"onClickCallback",{num:j,type:i});
                this.betContent[i].addChild(ball);
            }  
        }

    },

     onSelPageCallBack:function(toggle, customEventData){
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            this.rtDec.string = this._decContents[customEventData];
            this._curPage = customEventData;
            this.clearAllBetRecord();
            //this.showCurMiss();
        }  
    },

    onClickCallback:function(toggle, customEventData){
        var num = customEventData.num;
        var type = customEventData.type;
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            this._curSelects[type].push(num);
        }
        else
        {
             Utils.removeByValue(this._curSelects[type],num);
        }
        this.updateTgSelect();
        this.checkBet();
    },

    //检查倍数
    checkBet:function(){
        var bet = 0;
        var betTemp = 1;
        for(var i=0;i<5;i++)
        {
            betTemp = this._curSelects[i].length*betTemp;
        }
        bet = betTemp;
        this._sscManage.showAmount(bet);
    },

    //清空当前选择
    clearAllBetRecord:function(){
        for(var i=0;i<this._curSelects.length;i++)
        {
            this._curSelects[i].length = 0;
        }

        for(var i=0;i<this.betContent.length;i++)
        {
            var childs = this.betContent[i].children;
            for(var j=0;j<childs.length;j++)
            {
                childs[j].getComponent(cc.Toggle).isChecked = false;
            }
        }

        this.checkBet();
    },
   
    //机选
    randomSelectCallBack:function(){
        this.clearAllBetRecord();
        for(var i=0;i<5;i++)
        {
            var randomArray = Utils.getRandomArrayWithArray(this.betContent[i].children, 1);
            if(randomArray.length>0)
            {
                randomArray[0].getComponent(cc.Toggle).isChecked = true;
                var temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
                this.onClickCallback(randomArray[0],temp);
            }
        }    
        return true;
    },

    updateTgSelect:function(){
        for(var i=0;i<this._curSelects.length;i++)
        {
            if(this._curSelects[i].length > 0)
            {
                window.Notification.emit("updateTgSelect",false);
                return;
            }
        }
        window.Notification.emit("updateTgSelect",true);
    },

    //投注信息组合
    dataObj:function(){
        var objs = [];
        var obj = new Object(); 
        obj.PlayCode = parseInt(this._lotteryID + this.rules[this._curPage]); 
        var nums = "";
        var num = "";
        for(var i=0;i<5;i++)
        {
            num = "";
            Utils.sortNum(this._curSelects[i]);
            for(var j=0;j<this._curSelects[i].length;j++)
            {
                if(num != "")
                    num += ",";
                    num += this._curSelects[i][j];
            }
            if(nums != "")
                nums += "|";
            nums += num;
        }

        obj.num = nums;
        return obj;
    },
    
});

